package com.xqh.admin.controller.pms;

import com.xqh.admin.commom.BaseController;
import com.xqh.entity.pms.SysRole;
import com.xqh.entity.pms.SysRoleResource;
import com.xqh.service.pms.SysRoleService;
import com.xqh.base.mybatis.page.Page;
import com.xqh.base.util.response.RespMsg;
import com.xqh.base.util.serializer.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by leo on 2017/11/6.
 * 系统角色管理
 */
@RequestMapping("/pms/role")
@Controller
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService roleService;


    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ModelAndView list(){
        return new ModelAndView("pms/role/role_list");
    }

    @RequestMapping(value = "listData")
    public @ResponseBody Page listData(Page page, SysRole dto){
        roleService.findByPage(page, ObjectUtils.changeToMap(dto));
        return page;
    }

    @RequestMapping(value = "edit",method = RequestMethod.GET)
    public ModelAndView edit(ModelMap model,SysRole dto){
        if(null != dto.getId()){
           model.put("data",roleService.findById(dto.getId()));
        }
        return new ModelAndView("pms/role/role_edit");
    }

    @RequestMapping(value = "saveData",method = RequestMethod.POST)
    public @ResponseBody RespMsg<?> saveData(SysRole dto){
        try {
            putOperatorInfo(dto);
            roleService.saveData(dto);
        }catch (Exception e){
            return RespMsg.failResp(e.getMessage());
        }
        return RespMsg.successResp();
    }

    /**
     * 数据 启用  禁用
     * @param dto
     * @return
     */
    @RequestMapping(value = "modifyData",method = RequestMethod.POST)
    public @ResponseBody RespMsg<?> modifyData(SysRole dto){
        try {
            roleService.modifyData(dto);
        }catch (Exception e){
            return RespMsg.failResp(e.getMessage());
        }
        return RespMsg.successResp();
    }

    /**
     * 权限设置页面
     * @return
     */
    @RequestMapping(value = "resourceSetting",method = RequestMethod.GET)
    public ModelAndView resourceSetting(ModelMap model){
        model.put("roleCode",getString("roleCode"));
        return new ModelAndView("pms/role/role_resource_setup");
    }

    /**
     * 权限设置 数据保存
     * @return
     */
    @RequestMapping(value = "roleResourceSave",method = RequestMethod.POST)
    public @ResponseBody RespMsg<?> roleResourceSave(SysRoleResource dto){
        try {
            roleService.roleResourceSave(dto);
        }catch (Exception e){
            return RespMsg.failResp(e.getMessage());
        }
        return RespMsg.successResp();
    }

}
